<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('预计回款') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('回款时间')" prop="receivableDate">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />
      </el-form-item>
      <el-form-item :label="$t('回款主体')" prop="investorId">
        <el-select v-model="queryParams.investorId" value="" :placeholder="$t('回款主体')" clearable>
          <el-option
            v-for="investment in investmentList"
            :key="investment.id"
            :label="investment.investorName"
            :value="investment.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('回款类别')" prop="receivableType">
        <el-select v-model="queryParams.receivableType" :placeholder="$t('回款类别')" clearable>
          <el-option
            v-for="dict in receivableTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:receivable:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:receivable:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t('导出') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:receivable:edit']"
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >{{ $t('全覆盖导入') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dealExpectReceivableList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('回款时间')" align="center" prop="receivableDate" width="180">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ parseTime(scope.row.receivableDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('回款金额(元)')" align="center" prop="receivableAmount" width="180">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.receivableAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('回款主体')" align="center" prop="investorName" />
      <el-table-column :label="$t('SPV')" align="center" prop="spvName" />
      <el-table-column :label="$t('回款类别')" align="center" prop="receivableType" :formatter="receivableTypeFormat" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:receivable:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['deal:receivable:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预计回款对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealExpectReceivableForm ref="dealExpectReceivableForm" />
    </el-dialog>
    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" :close-on-click-modal="false" :lock-scroll="false" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">
          {{ $t('将文件拖到此处，或') }}
          <em>{{ $t('点击上传') }}</em>
        </div>
        <div slot="tip" class="el-upload__tip" style="color:red">{{ $t('提示：仅允许导入“xls”或“xlsx”格式文件！') }}</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" class="el-icon-download el-icon--right" style="font-size:12px;float:left;" @click="fetchTemplate">
          {{ $t('下载模板') }}
        </el-button>
        <el-button @click="upload.open = false">{{ $t("取消") }}</el-button>
        <el-button type="primary" @click="submitFileForm">{{ $t("确定") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDealExpectReceivable, delDealExpectReceivable } from '@/api/deal/estate/receivable/dealExpectReceivable'
import dealExpectReceivableForm from './dealExpectReceivableForm.vue'
import { listDealInvestment } from '@/api/deal/investment/dealInvestment'
import { getToken } from '@/utils/auth'
export default {
    components: {
        dealExpectReceivableForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 预计回款表格数据
          dealExpectReceivableList: [],
          // 项目投资主体数据
          investmentList: [],
          // 弹出层标题
          title: '预计回款',
          // 是否显示弹出层
          formOpen: false,
        // 回款类别字典
        receivableTypeOptions: [],
        // 日期查询范围
        dateRange: [],

          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              receivableDate: undefined,
              investorId: undefined,
              receivableType: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
        // 导入参数
        upload: {
          // 是否显示弹出层
          open: false,
          // 弹出层标题
          title: '',
          // 是否禁用上传
          isUploading: false,
          // 设置上传的请求头部
          headers: { Authorization: 'Bearer ' + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + 'receivable/dealExpectReceivable/importData/'
        }
      }
  },
  created() {
      this.getList()
    listDealInvestment({ 'dealId': this.$route.query.id }).then(response => {
      this.investmentList = response.rows
    })
      this.getDicts('deal_estate_collection_type').then(response => {
          this.receivableTypeOptions = response.data
      })
  },
  methods: {
    /** 查询预计回款列表 */
    getList() {
        this.loading = true
        listDealExpectReceivable(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.dealExpectReceivableList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 回款类别字典翻译
    receivableTypeFormat(row) {
      return this.selectDictLabel(this.receivableTypeOptions, row.receivableType)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,

            dealId: this.$route.query.id,

            receivableDate: undefined,

            receivableAmount: undefined,

            investorId: undefined,

            spvId: undefined,

            receivableType: undefined,

            createBy: undefined,

            createTime: undefined,

            updateBy: undefined,

            updateTime: undefined,

            delFlag: undefined

        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.dateRange = []
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealExpectReceivableForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealExpectReceivableForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealExpectReceivable(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/receivable/dealExpectReceivable/export', this.queryParams)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = this.$t('预计回款导入')
      this.upload.open = true
    },
    // 文件上传中处理
    handleFileUploadProgress() {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 下载模板
    fetchTemplate() {
      var href1 = window.location.href
      var href2 = this.$route.path
      var url = href1.substring(0, href1.indexOf(href2))
      window.location.href = url + '/template/projects-expectedRepaymentImportTemplate.xlsx'
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    }
  }
}
</script>
